import { http } from "@/utils/http";
import { sysUrlApi } from "../utils";

export type addOrgDto = {
  parentId: string;
  orgName: string;
  status: number;
};
// 添加部门
export const addOrg = (data: addOrgDto) => {
  return http.request("post", sysUrlApi("org/create"), { data });
};

// 获取部门列表
export const getOrgList = () => {
  return http.request<Array<any>>("get", sysUrlApi("org/list"));
};
// 获取映射的部门，也就是能管理的部门
export const getMapOrgList = () => {
  return http.request<Array<any>>("get", sysUrlApi("org/listDto"));
};

// 获取部门下的用户列表
export const getUserList = (orgId: string) => {
  return http.request<Array<any>>("get", sysUrlApi("org/userList/" + orgId));
};

// 删除部门
export const deleteOrg = (orgId: string) => {
  return http.request("delete", sysUrlApi("org/" + orgId));
};

type TreeNode = {
  name: string;
  type: number; // 1 公司 2 分公司 3 部门
  parentId: number;
  sort: number;
  leaderUserId: number;
  phone: string;
  email: string;
  status: number;
  id: number;
  createTime: number;
  creator: string;
  remark: string;
};

export const transitionTreeData = (r: Array<any>) => {
  const li: TreeNode[] = [];
  r.forEach(item => {
    if (item.id != "2") {
      li.push({
        name: item.orgName,
        type: 1, // 1 公司 2 分公司 3 部门
        parentId: item.parentId,
        sort: 0,
        leaderUserId: 1,
        phone: "",
        email: "",
        status: item.status,
        id: item.id,
        createTime: item.createdAt,
        creator: item.creator,
        remark: ""
      });
    }
  });
  return li;
};

export const simpleTransitionTree = data => {
  const cloneData = JSON.parse(JSON.stringify(data));
  return cloneData.filter(father => {
    const branchArr = cloneData.filter(
      child => father["deptId"] == child["parentId"]
    );
    branchArr.length > 0 ? (father["children"] = branchArr) : "";
    return father["parentId"] == "0";
  });
};
